package com.example.renttest.main.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.renttest.R;
import com.example.renttest.main.adapter.FoodAdapter;
import com.example.renttest.util.OkHttpUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import android.widget.AdapterView;
//import android.widget.Toast;

public class Zhujiemian extends AppCompatActivity  {
    private ListView lv;
    private List<Map<String,Object>> data;
    public static List<Map<String, Object>> car = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhujiemian);
        lv=findViewById(R.id.lv);
        init();


    }

    private void init() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                data=new ArrayList<>();
                try {
                    String result = OkHttpUtil.get("http://"+getResources().getString(R.string.ip)+"/foodserver/food/list");
                    JSONObject object = new JSONObject(result);
                    JSONArray list = object.getJSONArray("list");
                    Gson gson = new Gson();
                    Log.e("AAAA", "run: "+list);
                    data = gson.fromJson(list.toString(),new TypeToken<List<Map<String,Object>>>(){}.getType());
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        FoodAdapter adapter = new FoodAdapter(data);
                        lv.setAdapter(adapter);
                        car = adapter.car;
                    }
                });
            }
        }).start();
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, Menu.FIRST + 1, 1, "购物车");
        menu.add(Menu.NONE, Menu.FIRST + 2, 2, "退出登录");
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        /**
         * 菜单项被点击时调用，也就是菜单项的监听方法。
         * 通过这几个方法，可以得知，对于Activity，同一时间只能显示和监听一个Menu 对象。 TODO Auto-generated
         * method stub
         */

        switch (item.getItemId()) {
            case Menu.FIRST + 1:
                Intent intent = new Intent(Zhujiemian.this, shopCartActivity.class);
                startActivity(intent);
                break;
            case Menu.FIRST + 2:
                finish();
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

}
